package com.leetcode;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No240
 * @description 240 搜索二维矩阵 II
 * @date 2021/1/29 下午12:05
 */
public class No240 {
    public static void main(String[] args) {
        int[][] matrix = {{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
        int target = 5;
        int[][] matrix2 = {{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
        int target2 = 20;
        boolean res = searchMatrix(matrix2,target2);
        System.out.println(res);
    }

    public static boolean searchMatrix(int[][] matrix, int target) {
        if(matrix == null || matrix.length < 1 || matrix[0].length < 1){
            return false;
        }
        int row = matrix.length,col = matrix[0].length;
        int r = row - 1 ,c = 0;
        while (r >= 0 && c < col){
            if(matrix[r][c] == target){
                return true;
            }else if(matrix[r][c] < target){
                c++;
            }else{
                r--;
            }
        }
        return false;
    }

}
